var errorMsg="小宝自检中,请稍后再来看望小宝...";

var timer;

window.appModule.controller('LoginControl', function ($rootScope, $scope, $http, $state, ngNotify, $cookies,$interval) {
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });
    $interval.cancel(timer);
    $scope.countryList = [
        {value:'86',name:'+86'}
    ];
    $scope.selected = { areaCode: $scope.countryList[0] };
    $scope.login = function () {
        if("" == $scope.phoneNumber || undefined == $scope.phoneNumber){
            ngNotify.set('请输入手机号码', 'warn');
            return ;
        }
        if("" == $scope.password || undefined == $scope.password){
            ngNotify.set('请输入密码', 'warn');
            return ;
        }
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                $http({
                    method: 'POST',
                    url: ENV['API_URL']+'/loginByAccount',
                    data: {countryAreaCode:'86',phoneNumber: $scope.phoneNumber,password:$scope.password}
                }).success(function(data){
                    if(data.messageCode=='10000') {
                        // this callback will be called asynchronously
                        $rootScope.user = data.user;
                        $cookies.putObject('user', $rootScope.user);
                        $state.go("menu");
                    }else {
                        ngNotify.set(data.messageText, 'error');
                    }
                }).error(function(){
                    ngNotify.set(errorMsg, 'warn');
                });
            }
        });
    };
});

window.appModule.controller('LoginQRControl', function ($rootScope,$scope, $http, $state, ngNotify,$interval,$cookies) {
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });
    $http.post(ENV['API_URL']+'/openfaqserver/getQrCode.action').success(function(data){
        if(data.messageCode=='10000'){
            $scope.url=data.qrcodeStr;
            timer=$interval(function(){
                $http({
                    method: 'POST',
                    url: ENV['API_URL']+'/loginByQr',
                    data: {qrcodeStr: $scope.url},
                    ignoreLoadingBar: true
                }).success(function(data){
                    // this callback will be called asynchronously
                    if(data.messageCode=='10000') {
                        $rootScope.user = data.user;
                        $cookies.putObject('user', $rootScope.user);
                        $interval.cancel(timer);
                        $state.go("menu");
                    }
                    // when the response is available
                }).error(function(){
                    ngNotify.set(errorMsg, 'warn');
                    $interval.cancel(timer);
                });
            },2000);
        }
    }).error(function(){
        ngNotify.set(errorMsg, 'warn');
    });
});